package com.universe.titan.common.util;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;


/**
 * @Author : linmaobo
 * @create 2022/12/07
 */
@Slf4j
public class IpUtil {


    /**
     获取真实的ip
     @param request
     @return
     @throws
     */
    public static String getRealIp(HttpServletRequest request) {
        String ip;
        // 有的user可能使用代理，为处理用户使用代理的情况，使用x-forwarded-for
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        log.info("ip: {}", ip);
        if ("127.0.0.1".equals(ip)|| "0:0:0:0:0:0:0:1".equals(ip)) {
            try {
                // 获取本机真正的ip地址
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log.info("realIp: {}", ip);
        return ip;
    }






}
